//
//  ThrViewController.m
//  BGM
//
//  Created by liuqin on 2018/11/25.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "PushViewController.h"
#import "BGMHTTP.h"
#import "BGMUser.h"
#import "Common.h"
#import "PushCollectionViewCell.h"
#import "PushMessageViewController.h"

@interface PushViewController ()<UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,PushCollectionViewCellDelegate>

@property (nonatomic, strong)NSArray *dataArray;
@property (nonatomic, strong)UICollectionView *collectionView;



@end

@implementation PushViewController



- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
 
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    UITabBarItem *myitem = self.tabBarItem;
//    NSLog(@"sdfa");
    int  i = 0;
    for (UIView *tabBarButton in self.tabBarController.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            i = i+1;
            if (i==3) {
                CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
                animation.keyPath = @"transform.scale";
                animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
                animation.duration = 1;
                animation.calculationMode = kCAAnimationCubic;
                //把动画添加上去就OK了
                [tabBarButton.layer addAnimation:animation forKey:nil];
            }
        }
    }
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
   
    
    [self getType];
 
}
- (void)setTypeView:(NSArray *)array{
    self.dataArray = array;
    int section = (int)self.dataArray.count/4;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, SCREENH-section*(SCREENW/4+20)-self.tabBarController.tabBar.frame.size.height, SCREENW, section*(SCREENW/4+20)) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[PushCollectionViewCell class] forCellWithReuseIdentifier:@"PushCollectionViewCell"];
}
- (void)getType{
    NSDictionary *dic = @{@"userToken":[BGMUser shareInstance].userToken};
    
    [BGMHTTP requestWithMethodName:@"publish/getPublishType" parameters:dic completion:^(BOOL success, NSError *error, NSDictionary *result) {
        NSLog(@"type类型");
        dispatch_async(dispatch_get_main_queue(), ^{
            NSArray *array = [result objectForKey:@"data"];
            [self getData:array];
            
        });
    }];
    
}
- (void)getData:(NSArray *)array{
         [self setTypeView:array];
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
  
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifer = @"PushCollectionViewCell" ;
    
    
    PushCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifer forIndexPath:indexPath];
    
    if(cell == nil){
        cell = [[PushCollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, SCREENW/4, SCREENW/4+20)];
    }
    cell.pushCellDelegate = self;

    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
    cell.dic = dic;
     [cell.iconBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"push_%d",(int)indexPath.row+1]] forState:UIControlStateNormal];
    cell.iconTitle.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"name"]];
    return cell;
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREENW/4, SCREENW/4+20);
}
//
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0,0,0, 0);
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    return  YES;
}
- (void)clickCell:(NSDictionary *)dic{
    PushMessageViewController *vc = [[PushMessageViewController alloc]init];
    vc.typeArray = self.dataArray;
    vc.titleStr = [NSString stringWithFormat:@"%@",[dic objectForKey:@"name"]];
    vc.typeId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]];
//    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
    PushMessageViewController *vc = [[PushMessageViewController alloc]init];
    vc.typeArray = self.dataArray;
    vc.titleStr = [NSString stringWithFormat:@"%@",[dic objectForKey:@"name"]];
    vc.typeId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]];
//    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
